/*
 * MAX44009.h
 *
 *  Created on: Aug 28, 2020
 *      Author: daniel
 */

#ifndef BSP_ILLUMINATION_MAX44009_H_
#define BSP_ILLUMINATION_MAX44009_H_

#include "stm32l0xx.h"
#include "stdbool.h"

#define MAX44009_DEFAULT_ADDRESS    0x4A
#define MAX44009_ALT_ADDRESS        0x4B

// REGISTERS
#define MAX44009_INTERRUPT_STATUS   0x00
#define MAX44009_INTERRUPT_ENABLE   0x01
#define MAX44009_CONFIGURATION      0x02
#define MAX44009_LUX_READING_HIGH   0x03
#define MAX44009_LUX_READING_LOW    0x04
#define MAX44009_THRESHOLD_HIGH     0x05
#define MAX44009_THRESHOLD_LOW      0x06
#define MAX44009_THRESHOLD_TIMER    0x07

// CONFIGURATION MASKS
#define MAX44009_CFG_CONTINUOUS     0x80
#define MAX44009_CFG_MANUAL         0x40
#define MAX44009_CFG_CDR            0x08
#define MAX44009_CFG_TIMER          0x07

// ERROR CODES
#define MAX44009_OK		                 0
#define MAX44009_ERROR_WIRE_REQUEST    -10
#define MAX44009_ERROR_OVERFLOW        -20
#define MAX44009_ERROR_HIGH_BYTE       -30
#define MAX44009_ERROR_LOW_BYTE        -31



//float   max44009_getLux();
bool max44009_getLux(float* lux);

// check datasheet for detailed behavior
bool max44009_setConfiguration(uint8_t);
bool max44009_getConfiguration();
bool max44009_setAutomaticMode();
bool max44009_setContinuousMode();		// uses more power
bool max44009_clrContinuousMode();		// uses less power
// CDR = Current Divisor Ratio
// CDR = 1 ==> only 1/8th is measured
// TIM = Time Integration Measurement (table)
// 000  800ms
// 001  400ms
// 010  200ms
// 011  100ms
// 100   50ms       manual only
// 101   25ms       manual only
// 110   12.5ms     manual only
// 111    6.25ms    manual only
bool    max44009_setManualMode(uint8_t CDR, uint8_t TIM);
//int 	max44009_getIntegrationTime() { return 800 >> (max44009_getConfiguration() & 0x07); };  // ms


#endif /* BSP_ILLUMINATION_MAX44009_H_ */
